package descentFly.app
{
	import SEApp.se_util;
	
	import UIAway.uif_view;
	
	import UIFree.ObjectEvent;
	import UIFree.controls.uif_button;
	import UIFree.controls.uif_gbutton;
	import UIFree.controls.uif_popupMenu;
	import UIFree.uif_Timer;
	import UIFree.uif_component;
	import UIFree.uif_ecomponent;
	
	import away3d.events.Stage3DEvent;
	
	import descentFly.app.tools.df_dockAlign;
	import descentFly.app.tools.df_statusPanel;
	import descentFly.app.tools.df_toolDock;
	import descentFly.app.tools.df_toolRank;
	import descentFly.app.tools.df_weapontPanel;
	import descentFly.view.df_view;
	
	import flash.display.Stage;
	import flash.display.StageDisplayState;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	
	public class df_topTollbar extends df_toolDock {
		public static const HEIGHT: int = 45;
		public function df_topTollbar(parent:uif_component) {
			var inner: Rectangle = parent.getInner();
			super(parent, df_dockAlign.RIGHT, inner.x, inner.y, inner.width, HEIGHT - inner.y);
		}
		
		override protected function createComponents(layer:df_component, inner:Rectangle):void {
			var buttons: Array = [
				{
					caption		: locale.LEVELS,
					event		: df_events.SHOW_LEVELS,
					submenu		: [
						{
							caption	: locale.LEVELS,
							event	: df_events.SHOW_LEVELS
						},{
							caption	: locale.ONLINELEVELS,
							event	: df_events.SHOW_ONLINELEVELS
						}
					]
				}
			];
			if (!Space.player.source) 
				buttons.push({
					caption	: locale.OPTIONS,
					event	: df_events.SHOW_OPTIONS
				});
			
			createButtons(layer, buttons);
			//se_util.defButton(layer, se_util.DEFWIDTHBUTTON * layer.numChildren, 0, 'fullscreen', doFullScreenButton);

			new df_statusPanel(this, inner.height);
			new df_weapontPanel(this, inner.height);
			new df_toolRank(this, inner.height);

			/*
			_menu = new uif_popupMenu(app, {
				orientation	: uif_popupMenu.RIGHTPOSITION,
				itemClass	: uif_button,
				showEvent	: MouseEvent.ROLL_OVER
			}, 230, 200, 'popupmenu');
			
			var menuList: Array = [
				{caption: ' Смотреть и голосовать ', method: 'doView'}
			];
			
			if (data.uid == clt.externalServer.viewer_id)
				menuList.push({caption: ' Удалить коллаж ', method: 'doRemoveCollage'});
			else menuList.push({caption: ' Перейти в блог автора ', method: 'doBlog'});
			
			_menu.listMenu = menuList;
			
			
			
			_menu.activeControl = _button;
			_menu.addEvent(Event.SELECT, onSelect); 			*/
			
		}
		
		protected function doFullScreenButton(e: MouseEvent): void {
			if (stage.displayState != StageDisplayState.FULL_SCREEN) {
				
				function doContentCreate(e: Event): void {
					view.view.stage3DProxy.context3D.configureBackBuffer(stage.stageWidth, stage.stageHeight, 0);
					view.dispatchEvent(new ObjectEvent(uif_view.RENDERFREEZE, false));
				}
				
				var view: df_view = (dfApp as away_jiglib).view;
				
				view.dispatchEvent(new ObjectEvent(uif_view.RENDERFREEZE, true));
				
//				stage.addEventListener(Event.RESIZE, doResizeStage);
				view.view.stage3DProxy.addEventListener(Stage3DEvent.CONTEXT3D_CREATED, doContentCreate);
				stage.scaleMode 	= StageScaleMode.NO_SCALE;
				stage.displayState 	= StageDisplayState.FULL_SCREEN_INTERACTIVE;

				
				//stage.fullScreenSourceRect = new Rectangle(0,0, stage.fullScreenWidth, stage.fullScreenHeight);
				
			} else stage.displayState = StageDisplayState.NORMAL;
		}
		
		protected function createSubMenu(control: uif_ecomponent, subMenu: Array): void {
			function doRelease(e: ObjectEvent): void {
				app.dispatchEvent(new Event(e.object.event));
			}
			
			var _menu: uif_popupMenu = new uif_popupMenu(app, {
				orientation	: uif_popupMenu.BOTTOMPOSITION,
				itemClass	: uif_button,
				showEvent	: MouseEvent.ROLL_OVER
			}, se_util.DEFWIDTHBUTTON - 5, 200, 'popupmenu');
			
			_menu.listMenu = subMenu;
			_menu.activeControl = control;
			_menu.addEvent(Event.SELECT, doRelease);
			
		}
		
		protected function createButtons(layer:uif_ecomponent, buttons: Array): void {
			function doRelease(e: Event): void {
				app.dispatchEvent(new Event(buttons[e.target._data].event));
			}
			
			buttons.forEach(function(item: Object, i: int, arr: Array): void {
				var button: uif_gbutton = se_util.defButton(layer, se_util.DEFWIDTHBUTTON * i, 0, item.caption, doRelease, item.hint);
				button._data = i;
				if (item.submenu) createSubMenu(button, item.submenu);
			});
		}
		
	}
}